package com.example.icongyou.manager;


import com.example.icongyou.api.ApiService;
import com.example.icongyou.bean.ApiResponse;
import com.example.icongyou.bean.FileUrlResponse;
import com.example.icongyou.retrofit.RetrofitClient;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Call;

// KgApiManager.java - API 管理类（对应您的 JS 文件）
public class KgApiManager {
    private ApiService apiService;

    public KgApiManager() {
        this.apiService = RetrofitClient.getApiService();
    }

    // 获取文件 URL（直接返回 data 字段内容）
    public void getFileUrl(String fileName, ApiCallback<FileUrlResponse> callback) {
        Call<ApiResponse<FileUrlResponse>> call = apiService.getFileUrl(fileName);
        call.enqueue(new Callback<ApiResponse<FileUrlResponse>>() {
            @Override
            public void onResponse(Call<ApiResponse<FileUrlResponse>> call, Response<ApiResponse<FileUrlResponse>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    ApiResponse<FileUrlResponse> apiResponse = response.body();
                    if (apiResponse.isSuccess()) {
                        callback.onSuccess(apiResponse.getData());
                    } else {
                        callback.onError(apiResponse.getMsg());
                    }
                } else {
                    callback.onError("网络请求失败");
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<FileUrlResponse>> call, Throwable t) {
                callback.onError(t.getMessage());
            }
        });
    }

    // 获取视频 URL（返回完整响应，自行处理）
    public void getVideoUrl(String videoName, ApiCallback<ApiResponse<String>> callback) {
        Call<ApiResponse<String>> call = apiService.getVideoUrl(encodeUriComponent(videoName));
        call.enqueue(new Callback<ApiResponse<String>>() {
            @Override
            public void onResponse(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    callback.onSuccess(response.body());
                } else {
                    callback.onError("网络请求失败");
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                callback.onError(t.getMessage());
            }
        });
    }

    // 获取作业请求
    public void getHomeRequest(String videoName, ApiCallback<ApiResponse<Object>> callback) {
        Call<ApiResponse<Object>> call = apiService.getHomeRequest(encodeUriComponent(videoName));
        call.enqueue(new Callback<ApiResponse<Object>>() {
            @Override
            public void onResponse(Call<ApiResponse<Object>> call, Response<ApiResponse<Object>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    callback.onSuccess(response.body());
                } else {
                    callback.onError("网络请求失败");
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<Object>> call, Throwable t) {
                callback.onError(t.getMessage());
            }
        });
    }

    // 获取 PDF URL
    public void getPDFUrl(String pdfName, ApiCallback<ApiResponse<String>> callback) {
        Call<ApiResponse<String>> call = apiService.getPDFUrl(encodeUriComponent(pdfName));
        call.enqueue(createCallback(callback));
    }

    // 获取 PPT URL
    public void getPPTUrl(String pptName, ApiCallback<ApiResponse<String>> callback) {
        Call<ApiResponse<String>> call = apiService.getPPTUrl(encodeUriComponent(pptName));
        call.enqueue(createCallback(callback));
    }

    // 获取教材 URL
    public void getTextBookUrl(String textbookName, ApiCallback<ApiResponse<String>> callback) {
        Call<ApiResponse<String>> call = apiService.getTextBookUrl(encodeUriComponent(textbookName));
        call.enqueue(createCallback(callback));
    }

    // 通用回调创建方法
    private <T> Callback<ApiResponse<T>> createCallback(ApiCallback<ApiResponse<T>> callback) {
        return new Callback<ApiResponse<T>>() {
            @Override
            public void onResponse(Call<ApiResponse<T>> call, Response<ApiResponse<T>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    callback.onSuccess(response.body());
                } else {
                    callback.onError("网络请求失败");
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<T>> call, Throwable t) {
                callback.onError(t.getMessage());
            }
        };
    }

    // URL 编码（对应 JS 的 encodeURIComponent）
    private String encodeUriComponent(String component) {
        try {
            return URLEncoder.encode(component, "UTF-8")
                    .replaceAll("\\+", "%20")
                    .replaceAll("\\%21", "!")
                    .replaceAll("\\%27", "'")
                    .replaceAll("\\%28", "(")
                    .replaceAll("\\%29", ")")
                    .replaceAll("\\%7E", "~");
        } catch (UnsupportedEncodingException e) {
            return component;
        }
    }

    // 回调接口
    public interface ApiCallback<T> {
        void onSuccess(T result);
        void onError(String errorMessage);
    }
}
